import tensorflow as tf
from tensorflow import keras
import numpy as np
from datetime import datetime
logdir = "logs\\" + datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)
# 拟合数据的创建
x_data = np.linspace(-1, 1, 300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise
x_data = x_data.astype(np.float32)
noise = noise.astype(np.float32)
y_data = y_data.astype(np.float32)
# print(x_data.shape)
# 定义一个一个模型
model = keras.models.Sequential([
    keras.layers.Dense(10, activation='relu'),
    keras.layers.Dense(1, activation='linear')
])
model.compile(
    optimizer=tf.keras.optimizers.SGD(0.01),
    loss='mse')
model.fit(x_data, y_data, epochs=100, callbacks=[tensorboard_callback])